var constraintType;
var imgDir;

function setProperties(pImgDir,_constraintType){
	imgDir=pImgDir;
	constraintType=_constraintType;
}

function showDialogConstraints(tablename){
	if(!tablename){
		alert('Proporcione el nombre de la tabla antes de definir las restricciones');
		return;
	}
	controlsRestricciones=document.getElementById('controls_restricciones');
	options=controlsRestricciones.getElementsByTagName('input');
	
	for(i=0;i<options.length;i++){
		option=options[i];
		//alert(option.name+' '+option.value+' '+option.checked+' '+option.type);
		if(option.name=="constraintType" && option.checked){			
			constraintType=option.value;
			break;			
		}
	}
	
	if(!constraintType){
		alert('Debe seleccionar el tipo de restriccion');
		return;
	}
	
	switch (constraintType) {
	case 'pk':
		showDialog('pkunique.php','constraintType=pk&tablename='+tablename);
		break;
	case 'u':
		showDialog('pkunique.php','constraintType=u&tablename='+tablename);
		break;
	case 'fk':
		showDialog('fk.php','constraintType=fk&tablename='+tablename);
		break;
	case 'c':
		break;
	default:
		return;
	}	
}


function loadColumns(){
	tbl_columnas=window.opener.document.getElementById('tbl_columnas');
	elements=tbl_columnas.getElementsByTagName('input');
	rows=new Array();
	
	for(i=0,j=0;i<elements.length;i++){
		element=elements[i];
		if(element.name=='columnDefinition[]'){
			rows[j]=element.value.split('|');
			addColumn(rows[j][0],rows[j][1],rows[j][2]);			
			j++;
		}		
	}	
}


function addColumn(nombre, tipo,longitud) {
	tbl = document.getElementById('tbl_constraint');
	var lastRow = tbl.rows.length;
	var iteration = lastRow;
	var row = tbl.insertRow(lastRow);

	idGenerator=Math.random();
	
	row.id = idGenerator;

	var cell = row.insertCell(0);
	var cnombre = document.createTextNode(nombre);
	cell.appendChild(cnombre);

	if(longitud){
		tipo+='('+longitud+')';
	}
	
	var cell = row.insertCell(1);
	var ctipo = document.createTextNode(tipo);
	cell.appendChild(ctipo);

	var cell = row.insertCell(2);
	cell.innerHTML='<input type="checkbox" name="columns[]" value="'+nombre+'" id="columns[]">';
	
}

function validarPkUnique(){
	elements=document.getElementsByTagName("input");
	for(i=0;i<elements.length;i++){
		element=elements[i];
		if(element.name="columns[]"&&element.checked){
			return true;
		}		
	}
	alert('Debe seleccionar al menos una columna para forma la restriccion');
	return false;
}

function buildPkUnique(){
	if(!validarPkUnique()){
		return false;
	}	
	
	name=document.getElementById('cname').value;
	
	statement="CONSTRAINT "+name+" ";
	statement+=(constraintType=='pk')?"PRIMARY KEY ":"UNIQUE ";
	statement+="(";
	
	elements=document.getElementsByTagName('input');
	
	firstLoop=true;
	for(i=0,j=0;i<elements.length;i++){
		element=elements[i];
		if(element.id=='columns[]'&&element.checked){
			if(!firstLoop){
				statement+=',';	
			}
			statement+=element.value;
			firstLoop=false;
		}		
	}
	statement+=")";
		
	tbl = window.opener.document.getElementById('tbl_restricciones');
	var lastRow = tbl.rows.length;
	var iteration = lastRow;
	var row = tbl.insertRow(lastRow);

	idGenerator=Math.random();	
	row.id = idGenerator;

	var cell = row.insertCell(0);
	//var nombre = document.createTextNode(name);
	cell.innerHTML=name;

	var cell = row.insertCell(1);
	//var nombre = document.createTextNode(statement);
	cell.innerHTML=statement;
	
	var cell = row.insertCell(2);	
	
	var nombre = document.createElement("input");
	nombre.type="hidden";
	nombre.name="constraintDefinition[]";
	nombre.id="constraintDefinition"+idGenerator;
	
	deleteIconHtml='<a href="#" onclick="delRowRestricciones(\''+row.id+'\')">';
	deleteIconHtml+='<img src="'+imgDir+'/document_delete.png" title="Borrar" alt="Borrar" width="15" height="15" border="0"</a>';
	
	var hiddenFildHtml='<input type="hidden" name="constraintDefinition[]" id="constraintDefinition"'+idGenerator+' value="'+statement+'">';
	cell.innerHTML=deleteIconHtml+hiddenFildHtml;	
		
	if(constraintType=='pk'){
		window.opener.document.getElementById('constraintTypePk').disable=true;
	}
	
	window.close();
}

function delRowRestricciones(rowid) {	
	var tbl = document.getElementById('tbl_restricciones');
	var row=document.getElementById(rowid);
	tbl.removeChild(row);
}